<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	if(!$this->setting['zc_merch']){
		$_SESSION['forward_url']='jy_videomerch/myhome/';
		$_SESSION['merch_url']='jy_videomerch/merch/';
		$merch=model('jy_merch/model')->checkmerch();
		$merchid=$merch['id'];
	}else{
		$merchid=model('jy_merch/model')->checkadd_merch();
	}
	//查询在进行的项目
	$pid = intval($_RQ['pid']);
	$id = intval($_RQ['id']);
	$iscopy = intval($_RQ['iscopy']);
	if($pid){
		$tpl=pdo_get('jy_merch_game_item_tpl',array('uniacid'=>$_SESSION['uniacid'],'id'=>$pid));
		$gamename=$tpl['gamename'];
	}elseif($_RQ['gamename']){
		$gamename=$_RQ['gamename'];
	}elseif($id){
		$params=array('id'=>$id,'uniacid'=>$_SESSION['uniacid']);
		if(!$iscopy){
			$params['mid']=$_SESSION['uid'];
		}
		$return['post']=pdo_get('jy_merch_game_item',$params);
		$gamename=$return['post']['gamename'];
		if($iscopy){
			unset($return['post']['id']);
		}
	}
	if(!$gamename){
		exi('请选择要创建的活动类型！','error');
	}
	if(strpos($gamename,'tool_') !== false){
		$return['isdiyform']=1;
	}elseif(strpos($gamename,'game_') !== false){
		$return['isdiygoods']=1;
	}
	unset($tpl['id']);
	$user=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	//VIP时间：
	$user['vipstatus']=$user['viptime']>time()?1:2;
	if($id && !$iscopy){
		$this->session('pid',$id);
		$return['isid']=1;
	}else{
		$this->session('pid',0);
	}
	//$check=model('jy_merch/games')->checkup($user['id'],$gamename);
	if(!$check){
		//exi('','','/open/');
	}
	$return['likesnum']=pdo_getsum('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'likes');
	$return['looksnum']=pdo_getsum('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'looks');
	$return['itemnum']=pdo_count('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','credit1','credit2','mobile','groupid','avatar','nickname','createtime'));
	if($id){
		$return['post']['content']=json_decode($return['post']['content'],true);
		$return['post']['diyform']=json_decode($return['post']['diyform'],true);
		$return['post']['canyufileds']=json_decode($return['post']['canyufileds'],true);
		$return['post']['goods']=json_decode($return['post']['goods'],true);
		$return['post']['gameset']=json_decode($return['post']['gameset'],true);
		$return['post']['sendtypetitle']=$return['post']['sendtype']==1?'快递发货':$return['post']['sendtype']==2?'提货点自提':'没有物流';
		$return['post']['onlinetime']=date('Y-m-d',$return['post']['onlinetime']);
	}else{
		if($tpl){
			$return['post']=$tpl;
			$return['post']['goods']=json_decode($return['post']['goods'],true);
		}else{
			$return['post']=array('gamename'=>$gamename);
		}
		$pingxuanfileds =array(
			array('title'=>'选项','type'=>'1','ismust'=>'0','value'=>'','image'=>''),
			array('title'=>'选项','type'=>'1','ismust'=>'0','value'=>'','image'=>''),
		);
		$return['post']['content']=array(
			array('title'=>'文字','type'=>1),
		);
		$return['post']['diyform']=model('jy_quanhuo/fileds')->get_fileds($gamename);
		$return['post']['canyufileds']=model('jy_quanhuo/fileds')->get_canyufileds($gamename);
		$return['post']['share_set']='0';
		$return['post']['fansinfo_set']='0';
		$return['post']['msg_set']='0';
		$return['post']['sendtype']='0';
		$return['post']['onlinetime']=date('Y-m-d',time()+360*24*3600);
		$return['post']['gameset']=array('teamnum'=>2);
		$return['post']['goods']=array();
	}
	$return['post']['isdiyform']=$return['isdiyform'];
	$return['post']['merchid']=$user['id'];
	$return['newfileds']=model('jy_quanhuo/fileds')->get_fileds($gamename);
	$return['typeinfo']=$this->allitemgame[$gamename];
	$member['createtime']=date('Y-m-d',$member['createtime']);
	$return['member']=$member;
    $return['params']=array();
    $return['item']=array();
    $return['list']=array();
    $return['p_group']=pdo_getall('jy_merch_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	exi($return);
}
if($_RQ['op']=='post'){
	$id = $this->session('pid');
	$postdata=get_postdata();
	$pcd = $postdata['pcd'];
	$type = intval($postdata['type']);
	if(!$postdata['title']){
		exi('标题不能为空！','error');
	}
	if(!$postdata['video']){
		exi('视频不能为空！','error');
	}
	if(!$postdata['content']){
		exi('内容不能为空！','error');
	}
	if($_SESSION['wxapp_id'] && !$_SESSION['fromh5wxapp']){
			$result=model('core/wxapp_api')->msgSecCheck($postdata['title']);
			if($result['errcode']){
				exi('标题内容可能不合法！','error');
			}
			if($postdata['content'] && $postdata['content'][0] && $postdata['content'][0]['data']){
				$result=model('core/wxapp_api')->msgSecCheck($postdata['content'][0]['data']);
			}
			if($result['errcode']){
				exi('帖子内容可能不合法！','error');
			}
	}
	$is_show = $postdata['is_show']?1:0;
	$status = ($return['pluginset']['item_shenhe'] || $return['pluginset']['open_wxappsh'])?0:1;
	$onlinetime = intval(strtotime($postdata['onlinetime']))>time()?intval(strtotime($postdata['onlinetime'])):time()+180*24*3600;
	$data=array(
				'title'=>$postdata['title'],
				'logo'=>$postdata['logo'],
				'video'=>$postdata['video'],
				'content'=>ijson_encode($postdata['content']),
				'goods'=>ijson_encode($postdata['goods']),
				'diyform'=>ijson_encode($postdata['diyform']),
				'gameset'=>ijson_encode($postdata['gameset']),
				'canyufileds'=>ijson_encode($postdata['canyufileds']),
				'sendtype'=>intval($postdata['sendtype']),
				'share_set'=>intval($postdata['share_set']),
				'fansinfo_set'=>intval($postdata['fansinfo_set']),
				'onlinetime'=>$onlinetime,
				'msg_set'=>intval($postdata['msg_set']),
				'status'=>$status,
				'address'=>$postdata['address'],
				'mobile'=>$postdata['mobile'],
				'rednum'=>intval($postdata['rednum']),
				'noshow'=>intval($postdata['noshow']),
				'gamename'=>$postdata['gamename'],
	);
	if($id){
			pdo_update('jy_merch_game_item',$data,array('id'=>$id));
			if(!$status){
				exi('修改成功,请等待审核','','jy_videomerch/item//id='.$id);
			}else{
				exi('修改成功','success','jy_videomerch/item//id='.$id);
			}
	}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['gamename']=$postdata['gamename'];
			$data['mid']=$_SESSION['uid'];
			$data['merchid']=$postdata['merchid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_merch_game_item',$data);
			$id=pdo_insertid();
			if($postdata['payid']){
				$oid=pdo_getcolumn('core_paylog',array('id'=>intval($postdata['payid']),'uniacid'=>$_SESSION['uniacid']),'oid');
				pdo_update('jy_quanhuo_redlog',array('pid'=>$id),array('id'=>$oid,'uniacid'=>$_SESSION['uniacid']));
			}
			if(intval($postdata['noshow'])){
				exi('保存预览成功！','','jy_videomerch/item//id='.$id);
			}elseif($return['pluginset']['item_shenhe'] || $return['pluginset']['open_wxappsh']){
				exi('发布成功,请等待审核','','jy_videomerch/item//id='.$id);
			}else{
				exi('发布成功','','jy_videomerch/item//id='.$id);
			}
			
	}
}
if($_RQ['op']=='payred'){
	$post=get_postdata();
	if(!$post['wantmoney']){
		exi('请先设置充值金额！','error');
	}
	$fee=$post['wantmoney'];
	if($return['pluginset']['isideal_money']){
		if($fee<=0){
		   exi('红包金额需大于0元','error');
		}
	}else{
		if($fee<=1){
		   exi('红包金额需大于1元','error');
		}
		if($fee>200){
			   exi('单个红包金额最多不超过200元','error');
		}
		if($post['rednum']<3 || $post['rednum']>20){
			 exi('红包个数介于（包括）3-20之间','error');
		}
		if($fee/$post['rednum']<=1){
			 exi('红包平均数，不能小于1元','error');
		}
	}
	
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>0,
		'pid'=>0,
		'setmid'=>$_SESSION['uid'],
		'status'=>0,
		'fee'=>$fee,
		'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_quanhuo_redlog',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'payred',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'发送红包',
		'plugin'=>'jy_quanhuo',
		'back_url'=>'jy_videomerch/upitem/pay_result/'
	);
	$pay_id=cfc('pay')->create_pay_order($params);
	exi($pay_id);
}
if($_RQ['op']=='pay_result'){
	$redlog=pdo_get('jy_quanhuo_redlog',array('id'=>$_RQ['pay_result']['id'],'uniacid'=>$_SESSION['uniacid'],'setmid'=>$_SESSION['uid']),array('id','fee','status'));
	if($redlog['fee']!=$_RQ['pay_result']['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($redlog['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($_RQ['pay_result']['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('jy_quanhuo_redlog',array('status'=>1),array('id'=>$redlog['id'],'uniacid'=>$_SESSION['uniacid']));
	exi('充值成功！');
}